package com.yonyou.train.service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.yonyou.train.dao.CoursePreviewDao;
import com.yonyou.train.exception.PreviewServiceException;
import com.yonyou.train.model.*;
import com.yonyou.train.model.Class;
import com.yonyou.train.util.index.SearcherUtil;
import com.yonyou.train.yonyouec.MsgNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CoursePreviewService {
    @Autowired
    private CoursePreviewDao coursePreviewDao;

    public List queryCourse(String token){
        Auth auth =coursePreviewDao.findAuth(token);
        int signup=auth.getIssignup();
        if(signup==1){
            List list = new ArrayList();
            List<Class> course = coursePreviewDao.queryAllCourse(token);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd（E）HH:mm", Locale.CHINA);
            SimpleDateFormat s = new SimpleDateFormat("HH:mm");
//            List<Long> mapsignup=coursePreviewDao.findSignup(token);
            for (Class cour : course) {
                Map map = new HashMap();
                map.put("class_id", cour.getId());
                map.put("teacher", cour.getTeacher_name());
                map.put("assistant", cour.getAssistant_name());
                map.put("subject", cour.getSubject());
                map.put("teacher_id", cour.getTeacher_id());
                map.put("assistant_id", cour.getAssistant_id());
                int issignup=coursePreviewDao.findClass(cour.getId(),Long.parseLong(token));
                if(issignup!=0){
                    map.put("signup","1");
                }else {
                    map.put("signup","0");
                }


                List<Class_Sub> subCourse = coursePreviewDao.querySubCourse(cour.getId());
                if(subCourse.size()!=0){
                    List<Map> subinfo = new ArrayList<Map>();
                    for (Class_Sub sub : subCourse) {
                        Map m = new HashMap();
                        m.put("time", sdf.format(sub.getStarttime()).replaceAll("星期", "周") + "-" + s.format(sub.getEndtime()));
                        m.put("address", sub.getAddress());
                        m.put("subclass_id",sub.getId());


                        subinfo.add(m);

                    }
                    Map M=new HashMap();

                    M.put("class",map);
                    M.put("subclass",subinfo);



                    list.add(M);
                }


            }

            return list;
        }else{
            List list = new ArrayList();
            List<Class> course = coursePreviewDao.queryAllCourse1(token);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd（E）HH:mm", Locale.CHINA);
            SimpleDateFormat s = new SimpleDateFormat("HH:mm");

            for (Class cour : course) {
                Map map = new HashMap();
                map.put("class_id", cour.getId());
                map.put("teacher", cour.getTeacher_name());
                map.put("assistant", cour.getAssistant_name());
                map.put("subject", cour.getSubject());
                map.put("teacher_id", cour.getTeacher_id());
                map.put("assistant_id", cour.getAssistant_id());
                int issignup=coursePreviewDao.findClass(cour.getId(),Long.parseLong(token));
                if(issignup!=0){
                    map.put("signup","1");
                }else {
                    map.put("signup","0");
                }

                List<Class_Sub> subCourse = coursePreviewDao.querySubCourse1(cour.getId());
                if(subCourse.size()!=0){
                    List<Map> subinfo = new ArrayList<Map>();
                    for (Class_Sub sub : subCourse) {
                        Map m = new HashMap();
                        m.put("time", sdf.format(sub.getStarttime()).replaceAll("星期", "周") + "-" + s.format(sub.getEndtime()));
                        m.put("address", sub.getAddress());
                        m.put("subclass_id",sub.getId());


                        subinfo.add(m);

                    }
                    Map M=new HashMap();

                    M.put("class",map);
                    M.put("subclass",subinfo);



                    list.add(M);
                }


            }

            return list;
        }

    }

    public void addCourse(Map map){


        List<Integer>ids = (List) map.get("subclass_id");
        ArrayList<Map> stus = (ArrayList<Map>) map.get("students");

            for(Map students:stus){
                int i=(int)map.get("class_id");
                long ii=(long)i;
                int id= (int) students.get("user_id");
                long b = (long) id;
                int app =(int)map.get("applicant_id");
                long appp=(long)app;
            int aa=coursePreviewDao.findClass(ii,b);

            if(aa==0){
                for(Integer student_id:ids){
                    Class_Student cls=new Class_Student();



                    String a=(String) students.get("user_name");


                    cls.setUser_name(a);
                    cls.setUser_id(b);

                    map.get("class_id");




                    cls.setClass_id(ii );
                    cls.setApplicant_id(app );
                    cls.setSubclass_id(student_id);

                    coursePreviewDao.addCourse(cls);

                }
                if(id!=app){
                    ArrayList<String> to=new ArrayList<>();
                    to.add(String.valueOf(b));
                    String applicant_name=(String)map.get("applicant_name");
                    String class_name=(String)map.get("class_name");

                    MsgNotice msgNotice=new MsgNotice();
                    msgNotice.getMsg(to,applicant_name, class_name);
                }
            }continue;






            }





        }





    //查人员信息(按名字搜索)
    public List dofindUsers (String searchtext,String token,long class_id){
        List<User> users =new ArrayList<>();
        if(searchtext != null && searchtext.matches("[a-zA-Z]+")){
            List<String>names;
            names = SearcherUtil.getNames(searchtext.toLowerCase());
            if(names.size() != 0)
            users=coursePreviewDao.findUsersByPY(searchtext,token,names);
        }else{
             users =coursePreviewDao.findUsers(searchtext,token);
        }

        List<Map> list =new ArrayList();

        for(User user:users){
            Map map=new HashMap();
            long id=user.getId();
            map.put("user_id",user.getId());
            map.put("user_name",user.getName());
            map.put("dept_id",user.getDept_id());
            map.put("dept_name",user.getDept_name());
            map.put("avatar",user.getAvatar());
            map.put("workstatus",user.getWorkstatus());
            map.put("filed",user.getFiled());
            int a  = coursePreviewDao.findClass(class_id,id);
                if(a!=0){
                    map.put("issignup",1);
                }else{
                    map.put("issignup",0);
                }

            list.add(map);
        }
         return list;
    }

    //代人报名带出的默认人员（和用户同项目组）
    public List findUsersByDept(String token,long class_id){
        Auth auth =coursePreviewDao.findAuth(token);
        if(auth.getIssignup()==1) {
            List<User> users = coursePreviewDao.findUsersByDept(token);



            List<Map> list = new ArrayList();

            for (User user : users) {
                long id=user.getId();
                Map map = new HashMap();

                map.put("user_id", user.getId());
                map.put("user_name", user.getName());
                map.put("dept_id", user.getDept_id());
                map.put("dept_name", user.getDept_name());
                map.put("avatar", user.getAvatar());
                map.put("workstatus",user.getWorkstatus());
                map.put("filed",user.getFiled());
                int a  = coursePreviewDao.findClass(class_id,id);
                    if(a!=0){
                        map.put("issignup",1);
                    }else{
                        map.put("issignup",0);
                    }

                list.add(map);
            }
            return list;
        }else{
            throw new PreviewServiceException("对不起，您没有权限");
        }

    }

}
